import './assets/main.css'
import store from './store'
import { createApp } from 'vue'
import axios from 'axios'
import App from './App.vue'
import router from './router'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
// const webSocketService = require('./views/web_socket_service')
// // 开启服务端的监听, 监听客户端的连接
// // 当某一个客户端连接成功之后, 就会对这个客户端进行message事件的监听
// webSocketService.listen()
// 配置请求的跟路径
axios.defaults.baseURL = 'http://124.220.98.90:80/'
axios.interceptors.request.use((config) => {
  // console.log(config)
  config.headers.Authorization = window.sessionStorage.getItem('token') // 在最后必须 return config
  return config
})
const app = createApp(App)

app.use(ElementPlus)
app.config.globalProperties.$axios = axios
app.use(router)
app.use(store)
app.config.globalProperties.$http = axios //进行全局挂
app.config.globalProperties.$message = 'Message'
app.mount('#app')

var func_map = {}

function WsConnect(acc, tocken) {
  let ws = new WebSocket('ws://124.220.98.90:8033/wstest', [acc + '&' + tocken])
  ws.onopen = function () {
    console.log('连接')
  }

  ws.onmessage = function (res) {
    console.log('接收到的消息', res)
    var data = JSON.parse(res.data)
    console.log(data)
    if (data.event == 'ConnectSuccessEvent') {
      console.log('真的连接上了')
    } else if (data.event == 'UpdateTaskEvent') {
      console.log('UpdateTaskEvent接包')
      console.log(func_map)
      func_map['UpdateTaskEvent']()
    } else if (data.event == 'UpdateLeavemessageEvent') {
      func_map['UpdateLeavemessageEvent']()
    }
  }
}
export default {
  WsConnect,
  func_map
}
